import re
from collections import Counter


def analyzeBorderTown():
    """分析《边城》人物出场"""
    # 主要人物
    chars = ['翠翠', '爷爷', '傩送', '天保', '顺顺', '杨马兵']

    try:
        with open("BianCheng.txt", 'r', encoding='utf-8') as f:
            text = f.read()

        print("《边城》人物出场统计")
        print("=" * 35)

        results = []
        for char in chars:
            count = len(re.findall(char, text))
            results.append((char, count))

        # 按次数排序
        results.sort(key=lambda x: x[1], reverse=True)

        # 输出结果
        total = sum(count for _, count in results)
        for i, (char, count) in enumerate(results, 1):
            percent = (count / total) * 100
            print(f"{i}. {char}: {count}次 ({percent:.1f}%)")

    except FileNotFoundError:
        print("错误: 未找到 BianCheng.txt 文件")


# 运行分析
analyzeBorderTown()